/*
 * XThrobber.java, created on Oct 30, 2005
 * 
 * Copyright (c) 2005 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.components;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.xfc.XApp;
import org.xfc.eventbus.XProgressEvent;
import org.xfc.eventbus.XSubscriber;
import org.xfc.util.XResources;
import org.xfc.util.XUtils;

/**
 *
 *
 * @author Devon Carew
 */
public class XThrobber
	extends JPanel
{
	private JLabel label;
	
	private ImageIcon stillImage;
	private ImageIcon motionImage;
	
	private int animationCount;
	
	public XThrobber()
	{
		this(false);
	}
	
	/**
	 * 
	 */
	public XThrobber(boolean smallThrobber)
	{
		XResources resources = XApp.getApp().getResources();
		
		if (XUtils.getMajorJavaVersion() < 1.5)
		{
			// The firfox animated progress icons don't work in Java 1.4.
			// This bug has been fixed in JDK 1.5.
			stillImage = resources.getIcon("/misc/progress/indicator.white.still.gif");
			motionImage = resources.getIcon("/misc/progress/indicator.white.gif");
		}
		else
		{
			if (smallThrobber)
			{
				// TODO: fix these images so they animate in Java 1.4 as well -
				stillImage = resources.getIcon("/firefox/skin/classic/browser/Throbber-small.png");
				motionImage = resources.getIcon("/firefox/skin/classic/browser/Throbber-small.gif");
			}
			else
			{
				stillImage = resources.getIcon("/firefox/skin/classic/browser/Throbber.png");
				motionImage = resources.getIcon("/firefox/skin/classic/browser/Throbber.gif");
			}
		}
		
		label = new JLabel(stillImage);
		
		setLayout(new BorderLayout());
		add(label, BorderLayout.CENTER);
	}
	
	/**
	 * @param app
	 */
	public void listenToApp(XApp app)
	{
		// Listen to the app for progress messages.
		app.getEventBus().subscribe(new XSubscriber() {
			public void handleEvent(EventObject event) {
				XProgressEvent progress = (XProgressEvent)event;
				
				if (progress.getEventType() == XProgressEvent.INDETERMINATE_START)
					startThrobber();
				else if (progress.getEventType() == XProgressEvent.INDETERMINATE_END)
					stopThrobber();
			}
		}, XProgressEvent.class);
	}
	
	/**
	 * 
	 */
	public void startThrobber()
	{	
		animationCount++;
		
		if (animationCount == 1)
		{
			label.setIcon(motionImage);
		}
	}
	
	/**
	 * 
	 */
	public void stopThrobber()
	{	
		animationCount--;
		
		if (animationCount == 0)
		{
			label.setIcon(stillImage);
		}
	}
	
	public void toggleThrobbing()
	{
		if (isRunning())
			stopThrobber();
		else
			startThrobber();
	}
	
	/**
	 * @return Returns true if the throbber is animating.
	 */
	public boolean isRunning()
	{
		return animationCount > 0;
	}
	
	/**
	 * Turn on the throbber breifly.
	 */
	public void pulse()
	{
		Timer timer = new Timer(250, new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				stopThrobber();
			}
		});
		
		startThrobber();
		
		timer.start();
	}
	
}
